/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.login;

import beans.user.UserBean;
import database.Database;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Not Stephen
 */
public class LoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            Connection c = null;
            c = Database.getConnection();

            //Destination is set default to return (Safest)
            String destination = "./login.jsp";//Something was wrong, send back



            //CONNECTION WAS MADE \(^_^)/
            if (c != null) {
                //GET FORM PARAMETERS FROM REQUEST
                String email = request.getParameter("email");
                String password = request.getParameter("password");

                //MAKE STATEMENT
                PreparedStatement statement = c.prepareStatement("SELECT * FROM user WHERE email = ? AND password = ?");
                statement.setString(1, email);
                statement.setString(2, password);
                ResultSet rs = statement.executeQuery();

                //READ RESULT SET
                UserBean ub = null;
                while (rs.next()) {
                    //CREATE USER BEAN
                    ub = new UserBean(rs.getInt("id"), rs.getString("email"), rs.getString("password"), rs.getString("name"));
                }

                //PLACE BEAN INTO SESSION SCOPE
                if (ub != null) {
                    request.getSession().setAttribute("userBean", ub);
                    destination = "./accountpage.jsp";// CHANGE TO ACTUAL FORWARD LOCATION
                }

            }
            //FORWARD TO ACCOUNT PAGE
            RequestDispatcher rd = request.getRequestDispatcher(destination);//ACCOUNT PAGE.jsp?
            rd.forward(request, response);

            try {
                /* TODO output your page here. You may use following sample code. */
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet LoginServlet</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>THERE HAS BEEN AN ERROR</h1>");
                out.println("</body>");
                out.println("</html>");
            } finally {
                out.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
